package com.cljz.makepassenger.ui.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.base.BaseActivity;
import com.cljz.makepassenger.present.activity.About1qP;
import com.cljz.makepassenger.service.UpdataService;
import com.cljz.makepassenger.utils.T;
import com.cljz.makepassenger.utils.date.VerCodeUitl;
import com.jakewharton.rxbinding.view.RxView;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.droidlover.xdroidmvp.router.Router;
import rx.functions.Action1;

/**
 * Created by Administrator on 2017/4/7 0007.
 */

public class About1qActivity extends BaseActivity<About1qP> {


    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.txt_version_nunmber)
    TextView txtVersionNunmber;
    @BindView(R.id.txt_have_new_version)
    TextView txtHaveNewVersion;
    @BindView(R.id.layout_have_new_version)
    LinearLayout layoutHaveNewVersion;
    @BindView(R.id.fl_back)
    FrameLayout flBack;
    @BindView(R.id.tv_title_name)
    TextView tvTitleName;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;
    @BindView(R.id.circleImage)
    ImageView circleImage;
    @BindView(R.id.textView)
    TextView textView;
    private String myAppVersion;
    private String versioUrl;
    VerCodeUitl verCodeUitl;

    @Override
    public void initData(Bundle savedInstanceState) {
        tvTitleName.setText("关于1Q");
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_about_1q;
    }

    @Override
    public About1qP newP() {
        return new About1qP();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getLayoutId();
        ButterKnife.bind(this);
        //获取当前版本号
        //  txtVersionNunmber.setText(verCodeUitl.getVerCode(this));
        try {
            //获取当前版本号
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS);
            myAppVersion = packageInfo.versionName;
            if (!TextUtils.isEmpty(myAppVersion)) {
                txtVersionNunmber.setText(myAppVersion);
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void initListener() {
        super.initListener();
        //点击返回箭头
        RxView.clicks(flBack)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(1, TimeUnit.SECONDS).subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                exit();
            }
        });
        RxView.clicks(layoutHaveNewVersion)
                .compose(this.<Void>bindToLifecycle())
                .subscribe(new Action1<Void>() {
            @Override
            public void call(Void aVoid) {
                getP().About1q();
            }
        });
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        exit();
    }

    private void exit() {
        Router.newIntent(About1qActivity.this).to(MainActivity.class).putBoolean("showDrawer",true).launch();
        Router.pop(About1qActivity.this);
    }

    public void About1qSucceed() {
        if (TextUtils.isEmpty(versioUrl)) {
           // T.getInstance().showShort("当前已经是最新版本");
            showToast("当前已经是最新版本", R.mipmap.nearby_success);
        } else {
            if (Environment.getExternalStorageState().equals(
                    Environment.MEDIA_MOUNTED)) {
                Intent intent = new Intent(About1qActivity.this, UpdataService.class);
                startService(intent);
                T.getInstance().showShort("正在后台更新最新版本");
            } else {
                T.getInstance().showShort("SD卡不可用，请插入SD卡");
            }
        }
    }

    public void About1qFail(String err) {
//        T.getInstance().showShort(err);
    }
}
